﻿using System;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.IO;
using BankApplication.BusinessLogic;

namespace BankApplication.UI
{
	public class MiscUtils
	{
		public static void SerializeToXML(Object obj, string fileName)
		{
			XmlSerializer serializer = new XmlSerializer(obj.GetType());

			// TODO: consider revising the file mode
			using (var outputFile = new FileStream(fileName, FileMode.Create))
			{
				serializer.Serialize(outputFile, obj);
			}
		}

		public static string GetSaveFileName(string dialogCaption)
		{
			SaveFileDialog saveDialog = new SaveFileDialog();

			saveDialog.RestoreDirectory = true;
			saveDialog.Title = dialogCaption;
			saveDialog.ShowDialog();

			if (saveDialog.FileName == String.Empty)
			{
				throw new NoFileNameSpecifiedException();
			}

			return saveDialog.FileName;
		}
	}
}
